if(NOT userver_FIND_COMPONENTS)
    set(userver_FIND_COMPONENTS @USERVER_AVAILABLE_COMPONENTS@)
endif()
list(REMOVE_DUPLICATES userver_FIND_COMPONENTS)

cmake_policy(SET CMP0057 NEW) # IN LISTS support

foreach(ITEM_COMPONENT IN LISTS userver_FIND_COMPONENTS)
    if(TARGET userver::${ITEM_COMPONENT})
        get_target_property(is_imported userver::${ITEM_COMPONENT} IMPORTED)
        if(NOT is_imported)
            # Support `find_package(userver ...)` if userver was previously included as subdirectory
            set(${ITEM_COMPONENT}_FOUND TRUE)
        endif()
        if(${ITEM_COMPONENT}_FOUND)
            list(REMOVE_ITEM userver_FIND_COMPONENTS ${ITEM_COMPONENT})
        endif()
    endif()
endforeach()

if(NOT userver_FIND_COMPONENTS)
    # UserverSetupEnvironment.cmake was already included (otherwise the userver::universal target could not been build).
    userver_setup_environment()
    return()
endif()

if(CMAKE_BUILD_TYPE MATCHES "^.*Rel.*$") # same as in UserverSetupEnvironment
    include("${CMAKE_CURRENT_LIST_DIR}/release/userver-targets.cmake")
else()
    include("${CMAKE_CURRENT_LIST_DIR}/debug/userver-targets_d.cmake")
endif()

message(STATUS "Userver components: ${userver_FIND_COMPONENTS}")

include(CMakeFindDependencyMacro)

set(USERVER_DOWNLOAD_PACKAGES OFF)
set(USERVER_CMAKE_DIR ${CMAKE_CURRENT_LIST_DIR})
set(USERVER_TESTSUITE_DIR "${USERVER_CMAKE_DIR}/testsuite")
set(USERVER_CONAN @USERVER_CONAN@)
set(USERVER_IMPL_ORIGINAL_CXX_STANDARD @CMAKE_CXX_STANDARD@)
set(USERVER_IMPL_FEATURE_JEMALLOC @USERVER_FEATURE_JEMALLOC@)
set(USERVER_USE_STATIC_LIBS @USERVER_USE_STATIC_LIBS@)

if(USERVER_CONAN AND NOT DEFINED CMAKE_FIND_PACKAGE_PREFER_CONFIG)
    set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
endif()

set_property(GLOBAL PROPERTY userver_cmake_dir "${USERVER_CMAKE_DIR}")
if(NOT USERVER_CONAN)
    if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.15")
        list(PREPEND CMAKE_MODULE_PATH "${USERVER_CMAKE_DIR}/modules")
    else()
        list(APPEND CMAKE_MODULE_PATH "${USERVER_CMAKE_DIR}/modules")
    endif()
endif()

include("${USERVER_CMAKE_DIR}/UserverPreferStaticLibs.cmake")

foreach(ITEM_COMPONENT IN LISTS userver_FIND_COMPONENTS)
    include("${USERVER_CMAKE_DIR}/userver-${ITEM_COMPONENT}-config.cmake")
endforeach()

@PACKAGE_INIT@

check_required_components(userver)

if(USERVER_CMAKE_DIR IN_LIST CMAKE_MODULE_PATH AND NOT USERVER_CONAN)
    message(FATAL_ERROR "Don't flood cmake include namespace, please")
endif()

set(userver_FOUND TRUE)
